package com.ruoyi.develop.goodsSaledoGongxuUser.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSaledoGongxu.domain.GoodsSaledoGongxu;
import com.ruoyi.develop.goodsSaledoGongxu.service.IGoodsSaledoGongxuService;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.domain.GoodsSaledoGongxuUserMoney;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.IGoodsSaledoGongxuUserMoneyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsSaledoGongxuUser.domain.GoodsSaledoGongxuUser;
import com.ruoyi.develop.goodsSaledoGongxuUser.service.IGoodsSaledoGongxuUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单产品工序分配任务Controller
 * @author qh
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/goodsSaledoGongxuUser/goodsSaledoGongxuUser")
public class GoodsSaledoGongxuUserController extends BaseController {
    @Autowired
    private IGoodsSaledoGongxuUserService goodsSaledoGongxuUserService;
    @Autowired
    private IGoodsSaledoGongxuUserMoneyService iGoodsSaledoGongxuUserMoneyService;
    @Autowired
    private IGoodsSaledoGongxuService goodsSaledoGongxuService;

    /**
     * 查询订单产品工序分配任务列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSaledoGongxuUser entity) {
        startPage();
        List<GoodsSaledoGongxuUser> list = goodsSaledoGongxuUserService.selectList(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:query')")
    @GetMapping("/listHavedo")
    public TableDataInfo listHavedo(GoodsSaledoGongxuUser entity) {
        startPage();
        List<GoodsSaledoGongxuUser> list = goodsSaledoGongxuUserService.listHavedo(entity);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:query')")
    @GetMapping("/listHavedoGroup")
    public TableDataInfo listHavedoGroup(GoodsSaledoGongxuUser entity) {
        startPage();
        List<GoodsSaledoGongxuUser> list = goodsSaledoGongxuUserService.listHavedoGroup(entity);
        return getDataTable(list);
    }
    /**
     * 导出订单产品工序分配任务列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:export')")
    @Log(title = "订单产品工序分配任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSaledoGongxuUser entity) {
        List<GoodsSaledoGongxuUser> list = goodsSaledoGongxuUserService.selectList(entity);
        ExcelUtil<GoodsSaledoGongxuUser> util = new ExcelUtil<GoodsSaledoGongxuUser>(GoodsSaledoGongxuUser. class);
        util.exportExcel(response, list, "订单产品工序分配任务数据");
    }

    /**
     * 获取订单产品工序分配任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoGongxuUserService.selectById(id));
    }


    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:query')")
    @GetMapping(value = "/getByIdModify/{ids}")
    public AjaxResult getByIdModify(@PathVariable("ids") String[] ids) {
        return AjaxResult.success(goodsSaledoGongxuUserService.selectById(ids[0]));
    }
    /**
     * 新增订单产品工序分配任务
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:add')")
    @Log(title = "订单产品工序分配任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.insert(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:add')")
    @Log(title = "分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTask")
    public AjaxResult addUserTask(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTask(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:add')")
    @Log(title = "分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTaskZhuanpai")
    public AjaxResult addUserTaskZhuanpai(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTaskZhuanpai(entity));
    }

    @Log(title = "按计件批量分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTaskPiliang")
    public AjaxResult addUserTaskPiliang(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTaskPiliang(entity));
    }


    @Log(title = "按计时批量分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTaskJishiPiliang")
    public AjaxResult addUserTaskJishiPiliang(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTaskJishiPiliang(entity));
    }


    @Log(title = "杂活按计件分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTaskPiliangZahuo")
    public AjaxResult addUserTaskPiliangZahuo(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTaskPiliangZahuo(entity));
    }

    @Log(title = "杂活计时分配人员任务", businessType = BusinessType.INSERT)
    @PostMapping("/addUserTaskPiliangJishiZahuo")
    public AjaxResult addUserTaskPiliangJishiZahuo(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.addUserTaskPiliangJishiZahuo(entity));
    }
    /**
     * 修改订单产品工序分配任务
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:edit')")
    @Log(title = "订单产品工序分配任务", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:edit')")
    @Log(title = "订单产品工序批量分配任务", businessType = BusinessType.UPDATE)
    @PutMapping("/editAll")
    public AjaxResult editAll(@RequestBody GoodsSaledoGongxuUser entity) {
        if(entity.getIds().length>0){
            for(String id:entity.getIds()){
                entity.setId(id);
                goodsSaledoGongxuUserService.update(entity);
            }
            return success("批量修改完成！");
        }else{
            return error("批量修改失败！");
        }
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:edit')")
    @Log(title = "订单产品工序分配任务", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCounts")
    public AjaxResult editCounts(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.updateCounts(entity));
    }

    /**
     * 删除订单产品工序分配任务
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUser:remove')")
    @Log(title = "订单产品工序分配任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserService.deleteByIds(ids));
    }

    @DeleteMapping("/removeUpdate/{ids}")
    public AjaxResult removeUpdate(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserService.deleteByIdsUpdate(ids[0]));
    }
    /**
     * 修改工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:edit')")
    @Log(title = "批量修改工艺工序图纸号", businessType = BusinessType.UPDATE)
    @PutMapping("/editAllTuzhihao")
    public AjaxResult editAllTuzhihao(@RequestBody GoodsSaledoGongxuUser entity) {
        return toAjax(goodsSaledoGongxuUserService.editAllTuzhihao(entity));
    }

    @DeleteMapping("/deleteGxUser/{ids}")
    public AjaxResult deleteGxUser(@PathVariable String ids) {
        GoodsSaledoGongxuUser goodsSaledoGongxuUser=goodsSaledoGongxuUserService.selectById(ids);
        GoodsSaledoGongxuUserMoney goodsSaledoGongxuUserMoney=new GoodsSaledoGongxuUserMoney();
        goodsSaledoGongxuUserMoney.setParentId(goodsSaledoGongxuUser.getId());
        List<GoodsSaledoGongxuUserMoney> goodsSaledoGongxuUserMoneys=iGoodsSaledoGongxuUserMoneyService.selectList(goodsSaledoGongxuUserMoney);
        if(goodsSaledoGongxuUserMoneys.size()>0){
            return error("已分配任务不可修改或删除，请先删除分配任务的报工记录再删除！");
        }
        goodsSaledoGongxuUserService.deleteById(ids);
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
        if(goodsSaledoGongxu!=null){
            /*if((goodsSaledoGongxuUser.getCount().add(goodsSaledoGongxu.getFenpeiCount())).compareTo(goodsSaledoGongxu.getCounts())>0){
                goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getCounts());
                goodsSaledoGongxu.setRemainCount(BigDecimal.ZERO);
            }else{
                goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxuUser.getCount()));
                goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));
            }*/
            goodsSaledoGongxu.setFenpeiCount(goodsSaledoGongxu.getFenpeiCount().subtract(goodsSaledoGongxuUser.getCount()));
            goodsSaledoGongxu.setRemainCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFenpeiCount()));
            goodsSaledoGongxuService.update(goodsSaledoGongxu);
        }
        return success();
    }
}
